import md5 from '/utils/md5.js';

App({
  onLaunch(){
    let that = this;
    wx.getSystemInfo({
      success(res) {
        if (res.environment && res.environment == 'wxwork'){
          that.data.client = 2;
        }else{
          that.data.client = 1;
        }
      }
    })

    let appId = wx.getAccountInfoSync().miniProgram.appId;
    if (appId == "wx2ccbaad839779310") {
      that.data.program = 0;
    }
    if (appId == "wx696e253b2d218c64"){
      that.data.program = 1;
    }
  },

  post(url, params, result) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    let timestamp = new Date().getTime();
    let sign = this.signFun(timestamp)
    wx.request({
      url: url,
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/json',
        'Authorization':wx.getStorageSync('token'),
        'x-timestamp': timestamp,
        'x-client': this.data.client,
        'x-sign': sign
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.code == 501) {
          wx.reLaunch({
            url: '/pages/login/login',
          })
          return;
        }
        result(res.data);
      },
      fail: (res) => {
        wx.hideLoading();
        wx.showToast({
          title: '服务君走神了哦~~',
          icon: 'none',
          duration: 2000
        });
      },
    })
  },

  postNo(url, params, result) {
    let timestamp = new Date().getTime();
    let sign = this.signFun(timestamp)
    wx.request({
      url: url,
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/json',
        'Authorization':wx.getStorageSync('token'),
        'x-timestamp': timestamp,
        'x-client': this.data.client,
        'x-sign': sign
      },
      success: (res) => {
        //wx.hideNavigationBarLoading();
        wx.hideLoading();
        if (res.data.code == 501) {
          wx.reLaunch({
            url: '/pages/login/login',
          })
          return;
        }
        result(res.data);
      },
      fail: (res) => {
        wx.hideLoading();
        wx.showToast({
          title: '服务君走神了哦~~',
          icon: 'none',
          duration: 2000
        });
      },
    })
  },
  get(url, params, result) {
    let timestamp = new Date().getTime();
    let sign = this.signFun(timestamp)
    wx.showNavigationBarLoading();
    wx.request({
      url: url,
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('token'),
        'x-timestamp': timestamp,
        'x-client': this.data.client,
        'x-sign': sign
      },
      success: (res) => {
        wx.hideNavigationBarLoading();
        if (res.data.code == 501) {
          wx.reLaunch({
            url: '/pages/login/login',
          })
          return;
        }
        result(res.data);
      },
      fail: (res) => {
        wx.hideLoading();
        wx.showToast({
          title: '服务君走神了哦~~',
          icon: 'none',
          duration: 2000
        });
      },
    })
  },

  signFun(timestamp){
    let str = timestamp + this.data.secret
    return md5(str)
  },

  bezier: function (pots, amount) {
    var pot;
    var lines;
    var ret = [];
    var points;
    for (var i = 0; i <= amount; i++) {
      points = pots.slice(0);
      lines = [];
      while (pot = points.shift()) {
        if (points.length) {
          lines.push(pointLine([pot, points[0]], i / amount));
        } else if (lines.length > 1) {
          points = lines;
          lines = [];
        } else {
          break;
        }
      }
      ret.push(lines[0]);
    }
    function pointLine(points, rate) {
      var pointA, pointB, pointDistance, xDistance, yDistance, tan, radian, tmpPointDistance;
      var ret = [];
      pointA = points[0];//点击
      pointB = points[1];//中间
      xDistance = pointB.x - pointA.x;
      yDistance = pointB.y - pointA.y;
      pointDistance = Math.pow(Math.pow(xDistance, 2) + Math.pow(yDistance, 2), 1 / 2);
      tan = yDistance / xDistance;
      radian = Math.atan(tan);
      tmpPointDistance = pointDistance * rate;
      ret = {
        x: pointA.x + tmpPointDistance * Math.cos(radian),
        y: pointA.y + tmpPointDistance * Math.sin(radian)
      };
      return ret;
    }
    return {
      'bezier_points': ret
    };
  },

  data: {
    user: null,
    client:1,
    secret: "c6be4c72edc8db2324463c63de007a4f",
    program: 0,
    storeId:'',
    company: '',
  }
})

// {
//   "pagePath": "pages/store/store",
//   "text": "到店",
//   "iconPath": "images/store.png",
//   "selectedIconPath": "images/store_c.png"
// },

